/**
 * Classname :Utilities.java
 *
 * Version information: 1.0
 *
 * Date: Dec 28, 2010 ho.viet.duan create
 *
 * Planex Viet Name co.ltd, All Rights Reserved.
 */
package com.zho.web.common.context;

import com.zho.domain.model.CustomerInfo;
import com.zho.util.Logit;
import com.zho.web.security.AppWebUserDetails;
import com.zho.web.security.WebUserDetails;

import org.springframework.security.core.Authentication;
import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;


/**
 * @author ho.viet.duan
 * Created Dec 28, 2010
 */
public abstract class Utilities {
    private static Logit log = Logit.getInstance(Utilities.class);

    /**
     * 
     * @return
     */
    public static String getCurrentLoginUserName() {
        try {
            return SecurityContextHolder.getContext().getAuthentication().getName();
        } catch (Exception e) {
            log.debug("getCurrentLoginUserName() - " + e.getMessage(), e);
        }
        return null;
    }
    
    
    public static CustomerInfo getCurrentCustomerInfo() {

    	CustomerInfo currentCustomerInfo = null;
    	try {
        	WebUserDetails webUserDetails = Utilities.getWebUserDetails();
        	if (webUserDetails != null) {
        		AppWebUserDetails appWebUserDetails = (AppWebUserDetails) webUserDetails;
        		if (appWebUserDetails != null) {
        			currentCustomerInfo = appWebUserDetails.getUser();
        		}
        	}
        } catch (Exception e) {
            log.debug("getWebUserDetails() - " + e.getMessage(), e);
        }
        return currentCustomerInfo;
    }
    
    /**
     * get current vistar's user
     * 
     * @return
     */
    public static WebUserDetails getWebUserDetails() {
        try {
        	if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) {
	            Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	            if (obj != null && obj instanceof WebUserDetails) {
	                return (WebUserDetails) obj;
	            }
        	}
        } catch (Exception e) {
            log.debug("getWebUserDetails() - " + e.getMessage(), e);
        }
        return null;
    }

    /**
     * 
     * @return
     */
    public static String getCurrentLoginUserId() {
        try {
        	WebUserDetails webUserDetails = getWebUserDetails();
        	if (webUserDetails != null) {
        		return webUserDetails.getUserId();
        	}
        } catch (Exception e) {
            log.debug("getCurrentLoginUserId() - " + e.getMessage(), e);
        }
        return null;
    }
 

    /**
     * 
     * @return
     */
    public static SecurityContext getSecurityContext() {
        return SecurityContextHolder.getContext();
    }

    /**
     * check current user is annonymous or not
     * 
     * @return
     */
    public static boolean isAnonymous() {
        AuthenticationTrustResolver resolver = new AuthenticationTrustResolverImpl();
        SecurityContext ctx = SecurityContextHolder.getContext();
        if (ctx != null) {
            Authentication auth = ctx.getAuthentication();
            return resolver.isAnonymous(auth);
        }
        return true;
    }
    
}